package com.example.testdemo.teamView;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MrYan on 2018/8/22.
 * 群头像View，传入头像数组
 */

public class GroupAvatarView extends RelativeLayout {
    private static final String TAG = "GroupImageView";

    private Context context;
    private int count;
    private LayoutParams params;
    private List<String> imgUrlList;
    private int imgSize;
    private int space = 10;

    public GroupAvatarView(Context context) {
        super(context);
        init(context);
    }

    public GroupAvatarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GroupAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        space = getPaddingLeft();
        switch (count) {
            case 1:
                imgSize = Math.min(getWidth() - getPaddingLeft() - getPaddingRight(), getHeight() - getPaddingTop() - getPaddingBottom());
                break;
            case 2:
            case 3:
            case 4:
                imgSize = (Math.min(getWidth() - getPaddingLeft() - getPaddingRight(), getHeight() - getPaddingTop() - getPaddingBottom()) - space) / 2;
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                imgSize = (Math.min(getWidth() - getPaddingLeft() - getPaddingRight(), getHeight() - getPaddingTop() - getPaddingBottom()) - space * 2) / 3;
                break;
        }
    }

    private void init(Context context) {
        this.context = context;
        imgUrlList = new ArrayList<>();
    }

    /**
     * 设置群组头像
     */
    public void setImgUrlList(List<String> imgUrlList) {
        this.imgUrlList.clear();
        int count = imgUrlList.size() > 9 ? 9 : imgUrlList.size();
        for (int i = 0; i < count; i++) {
            this.imgUrlList.add(imgUrlList.get(i));
        }
        this.count = this.imgUrlList.size();
    }

    /**
     * 设置单个头像
     */
    public void setImgUrl(String imgUrl) {
        imgUrlList.add(imgUrl);
        count = 1;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (getChildCount() > 0) return;
        for (int i = 0; i < imgUrlList.size(); i++) {
            addImageView(i, imgUrlList.get(i));
        }
    }

    private void addImageView(int position, String url) {
        params = new LayoutParams(imgSize, imgSize);
        ImageView img = createImageView(url);
        switch (count) {
            case 1:
                break;
            case 2:
                Log.d(TAG, "addImageView: 图片大小：" + imgSize);
                if (position == 0) {
                    params.addRule(CENTER_VERTICAL);
                }
                if (position == 1) {
                    params.addRule(CENTER_VERTICAL);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
            case 3:
                if (position == 0) {
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 1) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 2) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
            case 4:
                if (position == 0) {
                }
                if (position == 1) {
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 2) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 3) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
            case 5:
                if (position > 1) {
                    params.setMargins(0, 0, 0, (imgSize + space) / 2);
                }
                if (position == 0) {
                    params.setMargins((imgSize + space) / 2, (imgSize + space) / 2, 0, 0);
                }
                if (position == 1) {
                    params.setMargins(0, (imgSize + space) / 2, (imgSize + space) / 2, 0);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 2) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 3) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 4) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }

                break;
            case 6:
                if (position < 3) {
                    params.setMargins(0, (imgSize + space) / 2, 0, 0);
                } else {
                    params.setMargins(0, 0, 0, (imgSize + space) / 2);
                }
                if (position == 0) {
                    params.addRule(ALIGN_PARENT_LEFT);
                }
                if (position == 1) {
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 2) {
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 3) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 4) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 5) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
            case 7:
                if (position == 0) {
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 1) {
                    params.addRule(CENTER_VERTICAL);
                }
                if (position == 2) {
                    params.addRule(CENTER_IN_PARENT);
                }
                if (position == 3) {
                    params.addRule(CENTER_VERTICAL);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 4) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 5) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 6) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
            case 8:
                if (position == 0) {
                    params.setMargins((imgSize + space) / 2, 0, 0, 0);
                }
                if (position == 1) {
                    params.setMargins(0, 0, (imgSize + space) / 2, 0);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 2) {
                    params.addRule(CENTER_VERTICAL);
                }
                if (position == 3) {
                    params.addRule(CENTER_IN_PARENT);
                }
                if (position == 4) {
                    params.addRule(CENTER_VERTICAL);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 5) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 6) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 7) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
            case 9:
                if (position == 0) {
                }
                if (position == 1) {
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 2) {
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 3) {
                    params.addRule(CENTER_VERTICAL);
                }
                if (position == 4) {
                    params.addRule(CENTER_IN_PARENT);
                }
                if (position == 5) {
                    params.addRule(CENTER_VERTICAL);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                if (position == 6) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                }
                if (position == 7) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(CENTER_HORIZONTAL);
                }
                if (position == 8) {
                    params.addRule(ALIGN_PARENT_BOTTOM);
                    params.addRule(ALIGN_PARENT_RIGHT);
                }
                break;
        }

        img.setLayoutParams(params);
    }

    private ImageView createImageView(String url) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Picasso.with(context).load(url).into(imageView);
        addView(imageView);
        return imageView;
    }
}
